package com.cs.base.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cs.base.util.LogCat;
import com.cs.base.util.ScreenUtil;


/**
 * 可拖拽图片
 */
public class DragImageView extends androidx.appcompat.widget.AppCompatImageView {

    private int mScreenHeight;
    private int mScreenWidth;
    private float touchedX;
    private float touchedY;
    private int movedX;
    private float movedY;
    private long startTime;
    private long endTime;
    private float startY;
    private boolean isFirst = true;
    private int statusBarHeight;

    public DragImageView(@NonNull Context context) {
        super(context);
        mScreenWidth = ScreenUtil.getWidthInPx(context);
        mScreenHeight = ScreenUtil.getHeightInPx(context);
//        LogUtils.e("获取高度，初始位置：" + startY);
    }

    public DragImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mScreenWidth = ScreenUtil.getWidthInPx(context);
        mScreenHeight = ScreenUtil.getHeightInPx(context);
//        LogUtils.e("获取高度，初始位置：" + startY);
    }

    public void setStatusBarHeight(int height){
        statusBarHeight = height;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                startTime = System.currentTimeMillis();
//                        smartRefreshLayout.setEnableRefresh(false);
                touchedX = getX() - event.getRawX();
                touchedY = getY() - event.getRawY();
                if (isFirst) {
                    isFirst = false;
                    startY = getY();
                    LogCat.e("DragImageView","获取高度，Y坐标：" + getY() + "---ROW Y ：" + event.getRawY() + "---纵坐标：" + touchedY);
                }
                break;
            case MotionEvent.ACTION_MOVE:
//                        LogUtils.e("获取高度，横坐标：" + x + "---纵坐标：" + y);
                setX(event.getRawX() + touchedX);
                setY(event.getRawY() + touchedY);
                break;
            case MotionEvent.ACTION_UP:
//                        smartRefreshLayout.setEnableRefresh(true);
//                        LogUtils.e("获取高度：位置：屏幕高度：" + mScreenHeight + "+++底部栏高度：" + BarUtils.getNavBarHeight() + "++Tab高度：" + BaseApplication.tabHeight + "===X:" + getX() + "==Y:" + getY());
                //处理左右的位置
                if (getX() < mScreenWidth / 2 - getWidth() / 2) {
                    movedX = 0;
                } else {
                    movedX = mScreenWidth - getWidth();
//                            LogUtils.e("获取高度：控件宽度===" + getWidth() + "===" + mScreenWidth);
                }
                setX(movedX);

                //处理上下的位置
                if (getY() < 0) {
                    movedY = 0 + statusBarHeight;
                    setY(movedY);
                } else if (getY() > startY) {
                    movedY = startY;
                    setY(startY);
                }
                endTime = System.currentTimeMillis();
                if ((endTime - startTime) < 100L) {//拖的不够给力，判断为点击
                    performClick();
                }
//                LogUtils.e("获取高度：最后：X:" + getX() + "+++ Y:" + getY() + "+++ 屏幕高度：" + mScreenHeight + "+++ Y:" + movedY);
                break;
            default:
                return false;
        }
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
